<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>学生信息管理</title>
  <link rel="stylesheet" href="css/index.css" />
  <style>
    p {
      width: 800px;
      margin: 20px auto;
      text-align: right;
    }
  </style>
</head>

<body>
  <h1>新增学员</h1>
  <form class="info" autocomplete="off">
    姓名：<input type="text" class="uname" name="uname" />
    年龄：<input type="text" class="age" name="age" />
    性别:
    <select name="gender" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资：<input type="text" class="salary" name="salary" />
    就业城市：<select name="city" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>
    </select>
    <button class="add">录入</button>
  </form>

  <p></p>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <!-- 
        <tr>
          <td>1001</td>
          <td>欧阳霸天</td>
          <td>19</td>
          <td>男</td>
          <td>15000</td>
          <td>上海</td>
          <td>
            <a href="javascript:">删除</a>
          </td>
        </tr> 
        -->
    </tbody>
  </table>
  <script>
    // 参考数据
    // const initData = [
    //   {
    //     stuId: 1001,
    //     uname: '欧阳霸天',
    //     age: 19,
    //     gender: '男',
    //     salary: '20000',
    //     city: '上海',
    //   }
    // ]
    const add = document.querySelector('.add');
    render()
    add.addEventListener('click', function (e) {
      e.preventDefault();
      let data = JSON.parse(localStorage.getItem('data')) ?? [];
      const id = (data[data.length - 1].id ?? 0) + 1;
      const name = document.querySelector('.uname').value;
      const age = document.querySelector('.age').value;
      const sex = document.querySelector('.gender').value;
      const salary = document.querySelector('.salary').value;
      const city = document.querySelector('.city').value;
      data.push({ id, name, age, sex, salary, city })
      localStorage.setItem('data', JSON.stringify(data))
      document.querySelector('.info').reset()
      render()
    })

    function render() {
      let data = JSON.parse(localStorage.getItem('data')) ?? [];
      let newData = data.map((v, k) => {
        let n =
          `
          <tr>
            <td>${v.id}</td>
            <td>${v.name}</td>
            <td>${v.age}</td>
            <td>${v.sex}</td>
            <td>${v.salary}</td>
            <td>${v.city}</td>
            <td><a href="javascript:" data-id="${k}">删除</a></td>
          </tr>
          `
        return n
      })
      document.querySelector('p').innerText = `当前学员数： ${data.length}`
      newData = newData.join('')
      document.querySelector('tbody').innerHTML = newData
    }

    const del = document.querySelector('tbody')
    del.addEventListener('click', function (e) {
      if (e.target.tagName === 'A') {
        if (confirm('您确定要删除这条数据嘛')) {
          $id = e.target.dataset.id
          let data = JSON.parse(localStorage.getItem('data'))
          data.splice($id, 1)
          localStorage.setItem('data', JSON.stringify(data))
          render()
        }
      }
    })
  </script>
</body>

</html>